package com.xcf.controller.business.common;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.ratiomanagement.RatioManagementService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/ratio")
public class RatioManagementController extends BaseController{
	@Resource
	private  RatioManagementService ratiomanagement;
	String menuUrl = "ratio/management "; // 菜单地址(权限用) 外币比率管理

@RequestMapping(value="management")
public ModelAndView ratio(){
	ModelAndView mv = new ModelAndView();
	mv.setViewName("/business/common/ratiomanagement/ratiomanagement");
	return mv;

}

@RequestMapping(value="/list")
public void list(HttpServletResponse response) throws Exception{
	PageData pd = new PageData();
	pd = this.getPageData();
	List<PageData> list=  ratiomanagement.sear(pd);
	JSONArray array = new JSONArray();
	for (PageData ration : list) {
		JSONObject ob = new JSONObject();
		ob.put("ID", ration.get("ID"));
		ob.put("CURRENCY", ration.getString("CURRENCY")); //币种
		ob.put("RATE", ration.get("RATE")); //比率
		if(ration.get("START_TIME")!=null&&ration.get("START_TIME")!=""){		
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
		 String str=sdf.format(ration.get("START_TIME"));
		 ob.put("START_TIME", str);	 //开始时间
	}else{
		 ob.put("START_TIME", ration.get("START_TIME"));	//开始时间
		}
		if(ration.get("START_TIME")!=null&&ration.get("START_TIME")!=""){	
		SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd");
		String END_TIME = a.format(ration.get("START_TIME"));
		ob.put("END_TIME", END_TIME);//结束时间
		}else{
			
			ob.put("END_TIME", ration.get("END_TIME"));//结束时间
		}
		
		ob.put("CHINESE_NAME", ration.getString("CHINESE_NAME"));//中文名
		String enable=null;
		if(ration.get("ENABLE")==null||ration.get("ENABLE").toString().equals("0")){
			enable="否";
		}else{
			enable="是";
		}
		ob.put("ENABLE", enable);//是否可用
		array.add(ob);
		
	}
	
	response.setContentType("text/html; charset=utf-8");         
    response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"50"+"\""+","+"\""+"rows"+"\""+":"+array+"}");
	
}

@RequestMapping(value="save")
@ResponseBody
public String save() throws Exception{
	
	PageData pd = new PageData();
	pd= this.getPageData();
	if(pd.getString("ID").isEmpty()){
		ratiomanagement.save(pd);
		}else{
		if(pd.getString("ENABLE").equals("是")){
			pd.put("ENABLE", 1);
		}else{
			pd.put("ENABLE", 0);
		}
		ratiomanagement.update(pd);
		}
	return null;
	
	
}
//删除
		@RequestMapping(value="/dele")
		@ResponseBody
		public String dele()throws Exception{
			PageData pd = new PageData();
			pd= this.getPageData();
			
			String data = pd.getString("array");
			
			JSONArray items = JSONArray.fromObject(data);
			for(int i=0,size=items.size();i<size;i++){
				JSONObject item = items.getJSONObject(i);
				String id = item.getString("id").trim();
				PageData pdq = new PageData();
				pdq.put("ID", id);
				ratiomanagement.delete(pdq);
			}
			return null;

}
}
